package cn.hesion.leetcode;
/**
 * ClassName: ReverseOnlyLetters <br/>
 * Description: <br/>
 * date: 2022/5/14 19:57<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

/**
 * @author: hesion
 * @create: 2022-05-14 19:57
 **/
public class ReverseOnlyLetters {
    public String reverseOnlyLetters(String s) {
        int n = s.length();
        char[] chars = s.toCharArray();
        int left = 0, right = n - 1;
        while (true) {
            while(left<right&&!Character.isLetter(s.charAt(left))){
                left++;
            }

            while (right>left&&!Character.isLetter(s.charAt(right))){
                right--;
            }

            if(left>=right){
                break;
            }
            swapLetterChar(chars,left,right);
            left++;
            right--;
        }
        return String.valueOf(chars);
    }

    private void swapLetterChar(char[] chars, int left, int right) {
        char tmp = chars[left];
        chars[left] = chars[right];
        chars[right] = tmp;
    }

    public static void main(String[] args) {
        String s = new ReverseOnlyLetters().reverseOnlyLetters("a-bC-dEf-ghIj");
        System.out.println(s);
    }
}
